package es.cloudcast.server.persistence.dao;

import java.io.Serializable;

import es.cloudcast.server.model.BasicModel;
import es.cloudcast.server.persistence.PersistenceException;

/**
 * Interface for basic CRUD operations for all model. 
 * 
 * @author Samuel García Martínez <in0de>
 *
 * @param <T> Persistence Class type
 * @param <PK> Primary Key class
 */
public interface BasicCRUDDAO<T extends BasicModel, PK extends Serializable> {

	/**
	 * Persiste given T transient instance
	 * 
	 * @param transientInstance instance to be persisted
	 * 
	 * @return T persisted instance.
	 * @throws PersistenceException if any error occurs
	 */
	public T create(T transientInstance) throws PersistenceException;
	
	/**
	 * Updates an already persisted instance.
	 * 
	 * @param persistentInstance instance to be updated
	 * 
	 * @return updated instance
	 * @throws PersistenceException if any error occurs
	 */
	public T update(T persistentInstance) throws PersistenceException;
	
	/**
	 * If instance is a transient instance it will be persisted, and
	 * if it is an already persisted it will be updated
	 * 
	 * @param instance to be persisted or updated
	 * 
	 * @return saved instance
	 * @throws PersistenceException if any error occurs 
	 */
	public T createOrUpdate(T instance) throws PersistenceException;
	
	/**
	 * Deletes persistenceInstance 
	 * 
	 * @param persistentInstance to be deleted 
	 * @throws PersistenceException if any error occurs
	 */
	public void delete(T persistentInstance) throws PersistenceException;
	
}
